package com.vehicle.controller;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.vehicle.bean.College;
import com.vehicle.form.CollegeForm;
import com.vehicle.service.CollegeService;
import com.vehicle.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-20
 */
@RestController
@RequestMapping("/college")
public class CollegeController {
    @Autowired
    CollegeService collegeService;

    //获取所有学院信息
    @GetMapping("/allList/{page}/{size}")
    public ResultVO getAllList(@PathVariable("page") String page,@PathVariable("size") String size){
        return ResultVO.succ(collegeService.getAllCollegeList(Integer.parseInt(page),Integer.parseInt(size)));
    }

    //获取所有学院信息
    @GetMapping("/")
    public ResultVO getCollegeOption(){
        return ResultVO.succ(collegeService.getCollegeOp());
    }

    //根据uniId获取学校对应学院id
    @GetMapping("/AllListByUniverId/{univerId}/{page}/{size}")
    public ResultVO getAllListByUniverId(@PathVariable("univerId") Integer univerId,@PathVariable("page") String page,@PathVariable("size") String size){
        return ResultVO.succ(collegeService.getAllListByUniverId(univerId,Integer.parseInt(page),Integer.parseInt(size)));
    }

    //添加学院
    @PostMapping("/")
    public boolean addCollege(HttpServletRequest request, @RequestBody CollegeForm collegeForm){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeService.insertCollege(ip,Integer.parseInt(id),collegeForm);
    }

    //获取学院名称
    @GetMapping("/getCollegeName/{collegeId}")
    public String getCollageNameById(@PathVariable("collegeId") Integer collegeId) {
        return collegeService.getNameById(collegeId);
    }

    //获取学院所属学校id
    @GetMapping("/getUniverId/{collegeId}")
    public Integer getUniverIdById(@PathVariable("collegeId") Integer collegeId) {
        return collegeService.getUniverId(collegeId);
    }

    //根据学院id修改学院信息
    @PutMapping("/")
    public Boolean updateCollage(HttpServletRequest request, @ModelAttribute College college){
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeService.updateTheCollage(ip,Integer.parseInt(id),college);
    }

    //根据学院id删除学院信息
    @DeleteMapping("/{collegeId}")
    public Boolean deleteCollege(HttpServletRequest request,@PathVariable("collegeId") Integer collegeId) {
        String ip = request.getRemoteAddr();
        String id = request.getHeader("id");
        return collegeService.deleteCollegeById(ip,Integer.parseInt(id),collegeId);
    }

}

